package com.shsxt.crm.controller.customer;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.shsxt.common.util.Result;
import com.shsxt.crm.model.YgCustomer;
import com.shsxt.crm.model.YgDataDic;
import com.shsxt.crm.service.ICustomerService;
import com.shsxt.framework.context.BaseController;

/**
 * 客户管理
 * 
 * @author Mr.YongGan.Zhang
 *
 */
@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {

	@Autowired
	private ICustomerService customerService;

	@GetMapping("/index.shtml")
	public String index() {
		return "customer";
	}

	@PostMapping("/queryAllCustomers")
	@ResponseBody
	public List<YgCustomer> queryAllCustomers() {
		return customerService.queryAllCustomers();
	}

	/**
	 * 查询客户信息
	 * 
	 * @return
	 * @param YgCustomer
	 */
	@PostMapping("/queryCustomersByParams")
	@ResponseBody
	public Map<String, Object> queryCustomersByParams(YgCustomer ygCustomer) {
		return customerService.queryCustomersByParams(ygCustomer);
	}

	/**
	 * 客户 等级查询
	 * 
	 * @param dataDicName
	 * @return
	 */
	@PostMapping("/queryDataDicValueByDataDicName")
	@ResponseBody
	public List<YgDataDic> queryDataDicValueByDataDicName(String dataDicName) {
		return customerService.queryDataDicValueByDataDicName(dataDicName);
	}

	@PostMapping("/insert")
	@ResponseBody
	public Result inertCustomerInfo(YgCustomer customer) {
		return customerService.insertCustomerInfo(customer);
	}

	/**
	 * 更新客户信息
	 * 
	 * @param customer
	 * @return
	 */
	@PostMapping("/update")
	@ResponseBody
	public Result updateCustomerInfo(YgCustomer customer) {
		return customerService.insertCustomerInfo(customer);
	}

	@PostMapping("/delete")
	@ResponseBody
	public Result deleteCustomerInfo(Integer[] ids) {
		return customerService.deleteCustomerInfo(ids);
	}

	/**
	 *   页面跳转
	 *   	联系人管理       type = 1
	 *      交往记录管理   type = 2
	 *      历史订单           type = 3
	 *        
	 * @return
	 */
	@GetMapping("/toOtherPage/{type}/{id}")
	public ModelAndView toOtherPage(@PathVariable ("type")Integer type ,  @PathVariable("id")Integer id) {
		ModelAndView mv = new ModelAndView();
		switch (type) {
		case 1:
			mv.setViewName("customer_linkman");
			break;
		case 2:
			mv.setViewName("customer_contact");
			break;
		case 3:
			YgCustomer customer = customerService.get(id);
			mv.addObject("customer", customer);
			mv.setViewName("customer_order");
			break;
		default:
			mv.setViewName("500");
		}
		return mv;
	}

}